package com.joven.core.controller;

import com.joven.core.constant.ResultInfo;
import com.joven.core.utils.lockUtil.ZkDistributedLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author joven_wong
 * 2021/6/30 10:07
 */
@RestController
@RequestMapping("/zk")
public class TestZKLockController {

    @Autowired
    ZkDistributedLock zkDistributedLock;

    @GetMapping("/test1")
    public ResultInfo zkLock1(){
        String path = "yu";
        boolean flag;
        zkDistributedLock.acquireLock(path);
        try {
            //业务代码
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            flag = zkDistributedLock.release(path);
        }
        flag = zkDistributedLock.release(path);
        return ResultInfo.success(flag);
    }

    @GetMapping("/test2")
    public ResultInfo zkLock2(){
        String path = "yu";
        boolean flag;
        zkDistributedLock.acquireLock(path);
        try {
            //业务代码
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            flag = zkDistributedLock.release(path);
        }
        flag = zkDistributedLock.release(path);
        return ResultInfo.success(flag);
    }

}
